/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZDataSource;
import br.zeos.util.POG;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.HashMap;
import javax.swing.JOptionPane;

/**
 *
 * @author virtual
 */
public class zData extends javax.swing.JPanel implements ActionListener {

    private ZDataSource ds = null;
    private String FieldData = "";
    private String FieldFileName = "";
    private byte[] data = null;
    private File arquivo = null;
    private boolean enabled = true;

    public void setEnabled(boolean value) {
        this.enabled = value;
        this.jButton1.setEnabled(this.enabled);
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public ZDataSource getDataSource() {
        return ds;
    }

    public void setDataSource(ZDataSource ds) {
        this.ds = ds;
    }

    public String getFieldData() {
        return FieldData;
    }

    public void setFieldData(String FieldData) {
        this.FieldData = FieldData;
        this.ds.addUpdateListener(this);
    }

    /**
     * Creates new form zImage
     */
    public zData() {
        JOptionPane.showMessageDialog(null, "Não use");
        System.exit(0);
        initComponents();
    }

    private void UpdateData() {
        try {
            HashMap<String, String> linha = this.ds.getLine();
            if (linha == null) {
                return;
            }
            linha.put(FieldData.toUpperCase(), POG.fileToSQL(arquivo));
            linha.put(FieldFileName.toUpperCase(), POG.fileToSQL(arquivo));
            this.ds.setLinha(linha);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/006.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/zeos/images/009.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("NOME");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.arquivo = POG.selectFile("", new String[]{""}, "Todos os arquivos");
        if (arquivo == null) {
            return;
        }
        this.FieldFileName = this.arquivo.getName();
        String hexa = POG.fileToSQL(this.arquivo);
        this.data = POG.sQLToFile(hexa);
        this.UpdateData();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        File local = POG.saveFile(System.getProperty("user.dir"));
        try {
            BufferedOutputStream saida = new BufferedOutputStream(new FileOutputStream(local));
            saida.write(data, 0, data.length);
            saida.flush();
            saida.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_jButton2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent ae) {
        String valor = this.ds.getLine().get(FieldData.toUpperCase());
        String nome = this.ds.getLine().get(FieldFileName.toUpperCase());
        if (valor == null) {
//            return;
        }
//        if (valor.isEmpty()) {
////            return;
//        }
//        this.imagePane1.setImage(POG.sQLToFile(valor));
        this.data = POG.sQLToFile(valor);

    }
}
